import datetime
import os
import re

from pip._vendor import requests

from utils.vendor.ip2Region import Ip2Region

"""
    服务统计统计接口调用次数/接口测试分布
"""

def push_robot(total_count, date, city_file, ip_file):
    headers = {"Content-Type": "text/plain"}
    s = "【服务器接口监控】\n统计时间:{}\n接口调用次数:{}\n城市分布:{}\nIP统计:{}".format(str(date), total_count, str(city_file),
                                                              str(ip_file))
    data = {
        "msgtype": "text",
        "text": {
            "content": s,
        },
        "mentioned_list": ["@all"],
    }
    requests.post(
        url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7ab8488f-4f0e-4f2d-82cb-da0b82710c3c',
        headers=headers, json=data)


def read_file(input_file_name, ip_file_name, city_file_name, date):
    with open(input_file_name) as _flog:
        total_count = 0
        ip_list = {}
        result = []
        for each in _flog:
            ip = re.findall(r'((?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d]))', str(each), re.S)
            if not ip:
                continue
            count = ip_list.get(ip[0], 0)
            if count > 0:
                ip_list[ip[0]] = count + 1
            else:
                ip_list[ip[0]] = 1
        ips = sorted(ip_list.items(), key=lambda d: d[1], reverse=True)
        ip_list.clear()
        searcher = Ip2Region("/etc/nginx/www/logutils/data/ip2region.db")
        for ip in ips:
            total_count = total_count + ip[1]
            addr = searcher.memorySearch(ip[0])
            city = addr['region'].decode('utf-8').split("|")
            result.append(ip + (city[0] + city[3],))
            with open(ip_file_name, "a+", encoding='utf-8') as _f1:
                _f1.writelines(str(ip + (city[0] + city[3],)) + "\n")

        for area in result:
            count = ip_list.get(area[2], 0)
            if count:
                ip_list[area[2]] = area[1] + count
            else:
                ip_list[area[2]] = area[1]

        result = sorted(ip_list.items(), key=lambda d: d[1], reverse=True)
        for city in result:
            with open(city_file_name, "a+", encoding='utf-8') as _f1:
                _f1.writelines(str(city) + "\n")

        push_robot(total_count, date, "https://smartgym.ocmcom.com/runtime/city_count" + date + ".log",
                   "https://smartgym.ocmcom.com/runtime/ip_count" + date + ".log")
        return "success"


if __name__ == '__main__':
    date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y%m%d")
    # 正式环境
    #input_file_name = "/var/log/nginx/log/smartgym_" + date + "_access.log"
    #ip_file_name = "/etc/nginx/www/smartgym/runtime/ip_count" + date + ".log"
    #city_file_name = "/etc/nginx/www/smartgym/runtime/city_count" + date + ".log"
    # 测试环境
    input_file_name = os.path.dirname(__file__)+'/data/smartgym.access.log'
    ip_file_name= os.path.dirname(__file__)+'/data/ip_count.log';
    city_file_name= os.path.dirname(__file__)+'/data/city_count.log';
    open(ip_file_name, 'w+')
    open(city_file_name, 'w+')

    print(read_file(input_file_name, ip_file_name, city_file_name, date))
